<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<jsp:include page="../../common/assets_header.jsp" />
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<script	src="<%=request.getContextPath()%>/assets/js/date-time/bootstrap-datepicker.min.js"></script>
<script	src="<%=request.getContextPath()%>/assets/js/jqGrid/jquery.jqGrid.min.js"></script>
<script	src="<%=request.getContextPath()%>/assets/js/jqGrid/i18n/grid.locale-en.js"></script>
<script>
jQuery(document).ready(function() {    
	loadGird();
});
//初始化列表
function loadGird(){
	var grid_selector = "#grid-table";//列表对象
	var pager_selector = "#grid-pager";//列表分页对象
	jQuery(grid_selector).jqGrid({
		url : "<%=request.getContextPath()%>/base/linkman/list_json",
		datatype : "json",
		height: 300,
		colNames:[ ' ','Company Code','First Name','Last Name','Address','Mobile','Telephone','Email','fax'],
		colModel:[
		{name:'baseCompanyLinkmanId',index:'baseCompanyLinkmanId', width:60, sorttype:"int", hidden: true},
		{name:'companyCode',index:'companyCode',width:90,sortable:false},
		{name:'firstName',index:'firstName', width:90,sortable:false},
		{name:'lastName',index:'lastName', width:90,sortable:false},
		{name:'address',index:'address', width:90,sortable:false},
		{name:'mobile',index:'mobile', width:90,sortable:false},
		{name:'telephone',index:'telephone', width:90,sortable:false},
		{name:'email',index:'email', width:90,sortable:false},
		{name:'fax',index:'fax', width:90,sortable:false}], 
		viewrecords : true,
		rowNum:10,
		rowList:[10,20,30],
		pager : pager_selector,
		altRows: true,
		multiselect: false,
		 multiboxonly: true,
		 loadComplete : function() {
			},
		caption: "Company Linkman List",
		autowidth: true
	});
}

function delLinkman(){
	 var id = $("#grid-table").jqGrid('getGridParam', 'selrow');
	 var obj = null;
	 if(id){
		 obj = $("#grid-table").jqGrid('getRowData', id);
	 }else{
		 bootbox.alert({  
	            buttons: {  
	               ok: {label: '确认',className: 'btn-ok'  }  
	            },  
	            message: '请选择一行数据.',  
	            title: "提示",  
	        });
		 return;
	}
	 var companyCode = obj.companyCode;
	 var id = obj.baseCompanyLinkmanId;
	bootbox.dialog({
		message : "<spring:message code="sure you want to delete"></spring:message>"+companyCode+"<spring:message code="what"></spring:message>",
		title : "<spring:message code="delete the this"></spring:message>",
		buttons : {
			confirm : {
				label : '<spring:message code="sure to delete"></spring:message>',
				className : "btn red btn-ok",
				callback : function() {
					$.ajax({
						type : "GET",
						url : "<%=request.getContextPath()%>/base/linkman/delete?id="+id,
						dataType : 'text',
						async : false,
						success : function(data) {
							if(data=="success"){
								$("#grid-table").trigger("reloadGrid"); 
							}else{
								noty({layout: 'center',text: "<spring:message code="delete the user failure"></spring:message>！",type: 'error', timeout: 2000 });
							}
						},
						error : function(data, status) {
							noty({layout: 'center',text: "<spring:message code="error"></spring:message>！",type: 'error', timeout: 2000 });
						}
					});
				}
			},
			cancel : {
				label : "<spring:message code="cancel"></spring:message>",
				className : "btn btn-danger"
			}
		}
	});
}

function add(){
	location.href="<%=request.getContextPath()%>/base/linkman/form";
}

function edit(){
	var id = $("#grid-table").jqGrid('getGridParam', 'selrow');
	 var obj = null;
	 if(id){
		 obj = $("#grid-table").jqGrid('getRowData', id);
		 location.href="<%=request.getContextPath()%>/base/linkman/form?id="+obj.baseCompanyLinkmanId;
	 }
}

function search(){
	var val = $("#searchId").val();
    var postData = $("#grid-table").jqGrid("getGridParam", "postData");
    $.extend(postData, {"searchValue":val});
	$("#grid-table").trigger("reloadGrid");
}

</script>
</head>

<body>
	<div class="page-content">
		<div class="page-header">
			<button class="btn btn-xs btn-primary" onclick="add();" id="newBtn">New</button>
			<button class="btn btn-xs btn-primary" onclick="edit();" id="editBtn">Edit</button>
			<button class="btn btn-xs btn-primary" onclick="delLinkman();" id="deleteBtn">Delete</button>
		</div>

		<div class="row">
			<div class="col-xs-12">
				<div class="form-group">
					<input type="text" id="searchId" placeholder  ="Company Code" maxlength="30" class="m-wrap" />
					<button class="btn btn-xs btn-primary"  onclick="search();"
						id="searchBtn">Search</button>
				</div>

				<!-- 列表 -->
				<table id="grid-table"></table>
				<!-- 分页 -->
				<div id="grid-pager"></div>
			</div>
		</div>
	</div>
</body>
</html>